#!/usr/bin/env python3
"""检查 patch 是否生效，并显示实际代码"""
import os
import sys

try:
    import diffusers
    target = os.path.join(os.path.dirname(diffusers.__file__), 
                         'models/transformers/transformer_z_image.py')
    
    if not os.path.exists(target):
        print(f"File not found: {target}")
        sys.exit(1)
    
    with open(target, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    
    # 查找问题行和 patch 代码
    print("Searching for relevant lines...")
    for i, line in enumerate(lines):
        if 'result.append(self.freqs_cis[i][index])' in line:
            print(f"\n=== FOUND ORIGINAL LINE at {i+1} ===")
            print(f"Line {i+1}: {line.rstrip()}")
            # 显示上下文
            start = max(0, i-5)
            end = min(len(lines), i+10)
            print("\nContext:")
            for j in range(start, end):
                marker = ">>> " if j == i else "    "
                print(f"{marker}{j+1:4d}: {lines[j].rstrip()}")
            break
        elif '_freqs_cis_npu_converted' in line:
            print(f"\n=== FOUND PATCH CODE at {i+1} ===")
            # 显示 patch 代码上下文
            start = max(0, i-2)
            end = min(len(lines), i+30)
            print("\nPatch code context:")
            for j in range(start, end):
                print(f"{j+1:4d}: {lines[j].rstrip()}")
            break
    
    # 检查是否有原始行
    has_original = any('result.append(self.freqs_cis[i][index])' in line for line in lines)
    has_patch = any('_freqs_cis_npu_converted' in line for line in lines)
    
    print(f"\n=== Summary ===")
    print(f"Has original line: {has_original}")
    print(f"Has patch code: {has_patch}")
    
    if has_original and has_patch:
        print("WARNING: Both original and patch code exist! This is wrong.")
    elif has_original:
        print("ERROR: Only original code exists, patch failed!")
    elif has_patch:
        print("OK: Patch code exists, but may have logic issues.")
        
except Exception as e:
    import traceback
    print(f"Error: {e}", file=sys.stderr)
    traceback.print_exc(file=sys.stderr)
    sys.exit(1)

